/**********************************************************************
 * SailmiPIM - modular cross-platform feature Personal Information Manager Tools.
 * Copyright(C) 2006-2014  Georg Rudoy
 *
 * Distributed under the Boost Software License, Version 1.0.
 *(See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
 **********************************************************************/

#include "structuresops.h"
#include <QtDebug>

QDataStream& operator<<(QDataStream& out, const SailmiPIM::Entity& e)
{
	quint16 version = 2;
	out << version
		<< e.Entity_
		<< e.Location_
		<< e.Mime_
		<< static_cast<quint32>(e.Parameters_)
		<< e.Additional_;
	return out;
}

QDataStream& operator>>(QDataStream& in, SailmiPIM::Entity& e)
{
	quint16 version = 0;
	in >> version;
	if(version == 2)
	{
		quint32 parameters = 0;
		in >> e.Entity_
			>> e.Location_
			>> e.Mime_
			>> parameters
			>> e.Additional_;

		if(parameters & SailmiPIM::NoAutostart)
			e.Parameters_ |= SailmiPIM::NoAutostart;
		if(parameters & SailmiPIM::DoNotSaveInHistory)
			e.Parameters_ |= SailmiPIM::DoNotSaveInHistory;
		if(parameters & SailmiPIM::IsDownloaded)
			e.Parameters_ |= SailmiPIM::IsDownloaded;
		if(parameters & SailmiPIM::FromUserInitiated)
			e.Parameters_ |= SailmiPIM::FromUserInitiated;
		if(parameters & SailmiPIM::DoNotNotifyUser)
			e.Parameters_ |= SailmiPIM::DoNotNotifyUser;
		if(parameters & SailmiPIM::Internal)
			e.Parameters_ |= SailmiPIM::Internal;
		if(parameters & SailmiPIM::NotPersistent)
			e.Parameters_ |= SailmiPIM::NotPersistent;
		if(parameters & SailmiPIM::DoNotAnnounceEntity)
			e.Parameters_ |= SailmiPIM::DoNotAnnounceEntity;
		if(parameters & SailmiPIM::OnlyHandle)
			e.Parameters_ |= SailmiPIM::OnlyHandle;
		if(parameters & SailmiPIM::OnlyDownload)
			e.Parameters_ |= SailmiPIM::OnlyDownload;
		if(parameters & SailmiPIM::AutoAccept)
			e.Parameters_ |= SailmiPIM::AutoAccept;
		if(parameters & SailmiPIM::FromCommandLine)
			e.Parameters_ |= SailmiPIM::FromCommandLine;
	}
	else if(version == 1)
	{
		QByteArray buf;
		quint32 parameters = 0;
		in >> buf
			>> e.Location_
			>> e.Mime_
			>> parameters
			>> e.Additional_;

		e.Entity_ = buf;

		if(parameters & SailmiPIM::NoAutostart)
			e.Parameters_ |= SailmiPIM::NoAutostart;
		if(parameters & SailmiPIM::DoNotSaveInHistory)
			e.Parameters_ |= SailmiPIM::DoNotSaveInHistory;
		if(parameters & SailmiPIM::IsDownloaded)
			e.Parameters_ |= SailmiPIM::IsDownloaded;
		if(parameters & SailmiPIM::FromUserInitiated)
			e.Parameters_ |= SailmiPIM::FromUserInitiated;
		if(parameters & SailmiPIM::DoNotNotifyUser)
			e.Parameters_ |= SailmiPIM::DoNotNotifyUser;
		if(parameters & SailmiPIM::Internal)
			e.Parameters_ |= SailmiPIM::Internal;
		if(parameters & SailmiPIM::NotPersistent)
			e.Parameters_ |= SailmiPIM::NotPersistent;
		if(parameters & SailmiPIM::DoNotAnnounceEntity)
			e.Parameters_ |= SailmiPIM::DoNotAnnounceEntity;
		if(parameters & SailmiPIM::OnlyHandle)
			e.Parameters_ |= SailmiPIM::OnlyHandle;
		if(parameters & SailmiPIM::OnlyDownload)
			e.Parameters_ |= SailmiPIM::OnlyDownload;
		if(parameters & SailmiPIM::AutoAccept)
			e.Parameters_ |= SailmiPIM::AutoAccept;
		if(parameters & SailmiPIM::FromCommandLine)
			e.Parameters_ |= SailmiPIM::FromCommandLine;
	}
	else
	{
		qWarning() << Q_FUNC_INFO
			<< "unknown version"
			<< "version";
	}
	return in;
}

namespace SailmiPIM
{
	bool operator<(const SailmiPIM::Entity& e1, const SailmiPIM::Entity& e2)
	{
		return e1.Mime_ < e2.Mime_ &&
			e1.Location_ < e2.Location_ &&
			e1.Parameters_ < e2.Parameters_;
	}

	bool operator==(const SailmiPIM::Entity& e1, const SailmiPIM::Entity& e2)
	{
		return e1.Mime_ == e2.Mime_ &&
			e1.Entity_ == e2.Entity_ &&
			e1.Location_ == e2.Location_ &&
			e1.Parameters_ == e2.Parameters_ &&
			e1.Additional_ == e2.Additional_;
	}
}
